<template>
    <div class="progress-bar" ref="progressBar" @click="progressClick">
      <div class="bar-inner">
        <div class="progress" ref="progress"></div>
       <!-- prevent组织浏览器的默认事件-->
        <div
          class="progress-btn-wrapper"
          ref="progressBtn"
          @touchstart.prevent="progressTouchStart"
          @touchmove.prevent="progressTouchMove"
          @touchend.prevent="progressTouchEnd"
        >
          <div class="progress-btn"></div>
        </div>
      </div>
    </div>
</template>

<script>
import {prefixStyle} from 'common/js/dom'
const progressBarWidth = 16
const transform = prefixStyle('transform')
export default {
  name: 'progressBar',
  props: {
    percent: {
      type: Number,
      default: 0
    }
  },
  created () {
    // 通过共享数据，把数据映射到touch上面
    this.touch = {}
  },
  watch: {
    percent (newPercent) {
      // 判断优先级
      if (newPercent >= 0 && !this.touch.initiated) {
        const barWidth = this.$refs.progressBar.clientWidth - progressBarWidth
        // 偏移量
        const offsetWidth = newPercent * barWidth
        this._offset(offsetWidth)
      }
    }
  },
  methods: {
    progressTouchStart (e) {
      // 定义点击了
      this.touch.initiated = true
      // 表示我们点击事件中第一个点击的位置,pageX表示横向的坐标
      this.touch.startX = e.touches[0].pageX
      this.touch.left = this.$refs.progress.clientWidth
    },
    progressTouchMove (e) {
      if (!this.touch.initiated) {
        return
      }
      const deltaX = e.touches[0].pageX - this.touch.startX
      const offsetWidth = Math.min(this.$refs.progressBar.clientWidth - progressBarWidth, Math.max(0, this.touch.left + deltaX))
      this._offset(offsetWidth)
    },
    progressTouchEnd (e) {
      this.touch.initiated = false
      this._triggerPercent()
    },
    _triggerPercent () {
      const barWidth = this.$refs.progressBar.clientWidth - progressBarWidth
      const percent = this.$refs.progress.clientWidth / barWidth
      // 派发事件
      this.$emit('percentChange', percent)
    },
    // 进行一个函数封装，简化代码量
    _offset (offsetWidth) {
      this.$refs.progress.style.width = `${offsetWidth}px`
      this.$refs.progressBtn.style[transform] = `translate3d(${offsetWidth}px, 0, 0)`
    },
    // 点击跳转到进度条的位置
    progressClick (e) {
      this._offset(e.offsetX)
      this._triggerPercent()
    }
  }
}
</script>

<style lang="stylus" scoped>
  @import "~common/stylus/variable"
  .progress-bar
    height 30px
    .bar-inner
      position relative
      top 13px
      height 4px
      background rgba(0, 0, 0, .3)
      .progress
        position absolute
        height 100%
        background $color-theme
      .progress-btn-wrapper
        position absolute
        left -8px
        top -13px
        width 30px
        height 30px
        .progress-btn
          position relative
          top 7px
          left 7px
          box-sizing border-box
          width 16px
          height 16px
          border 3px solid $color-text
          border-radius 50%
          background $color-theme
</style>
